package com.aaa.service.impl;

import com.aaa.entity.TabRole;
import com.aaa.entity.TabUser;
import com.aaa.entity.TabUserRole;
import com.aaa.mapper.TabMenuMapper;
import com.aaa.mapper.TabRoleMapper;
import com.aaa.mapper.TabUserMapper;
import com.aaa.mapper.TabUserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyUserServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private TabUserMapper tabUserMapper;

    @Autowired
    private TabUserRoleMapper userRoleMapper;

    @Autowired
    private TabRoleMapper tabRoleMapper;

    @Autowired
    private TabMenuMapper tabMenuMapper;
    //
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // username   指的是 你在表单中输入的用户名
        // 根据你输入的用户名去数据库中查询是否有这个用户
        QueryWrapper queryWrapper = new QueryWrapper();// 条件查询的构造器
        queryWrapper.eq("username", username);// eq 代表的是等值查询  where username = “zs”

        TabUser tabUser = tabUserMapper.selectOne(queryWrapper);// 查询的结果只有一个   username 是不能重复的
        // tabUser
        if (tabUser == null) {
            System.out.println(username+"信息不存在！！！！");
            throw new UsernameNotFoundException(username);
        }else{
           // List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
            // 角色  角色ROLE_为开始
            // 查询当前用户的角色的rcode
            // 当前用户的id
            Integer id = tabUser.getId();
            // 根据用户的id 去查角色的rcode
            // select rid from tab_user_role WHERE uid=1;
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("uid", id);
            //  select * from tab_user_role WHERE uid=1;
            List<TabUserRole> list = userRoleMapper.selectList(queryWrapper2);

            // stream 流
            List<Integer> rids = list.stream().map(tabUserRole -> tabUserRole.getRid()).collect(Collectors.toList());
            System.out.println(rids);
            //SELECT rcode from tab_role where id IN(1,2);
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.in("id",rids);
            List<TabRole> list1 = tabRoleMapper.selectList(queryWrapper1);
            List<SimpleGrantedAuthority> authorities  = list1.stream().map(tabRole -> new SimpleGrantedAuthority("ROLE_"+tabRole.getRcode())).collect(Collectors.toList());

            // 用户的菜单的资源也在authorities里面
            // 角色的id  rids
            //  查询菜单的资源
            List<String> menus= tabMenuMapper.findMenuCodeByRids(rids);
           // System.out.println("菜单的code"+menus);
//  List<SimpleGrantedAuthority>
//            menus.stream().map(s->new SimpleGrantedAuthority(s)).forEach(authorities::add);
            for (String menu : menus) {
                authorities.add(new SimpleGrantedAuthority(menu));
            }
            return new User(username, tabUser.getPassword(),authorities);
        }

    }
}
